//
//  YKUserDefault.m
//  YK_BaseTools
//
//  Created by edward on 2021/1/7.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKUserDefault.h"

@interface YKUserDefault ()
///存储default
@property(nonatomic,strong) NSUserDefaults *userdefaults;
///once队列
@property(nonatomic,strong) NSMutableArray<NSString *> *onceKeysArray;
///normal队列
@property(nonatomic,strong) NSMutableArray<NSString *> *normalKeysArray;

@end

static YKUserDefault *instance = nil;

@implementation YKUserDefault

static YKUserDefault *_instance;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });

    return _instance;
}

+ (instancetype)sharedInstance
{
    if (_instance == nil) { 
        _instance = [[YKUserDefault alloc] init];
    }

    return _instance;
}

+ (void)setOnceValue:(id)value forKey:(NSString *)key
{
    if (![[YKUserDefault sharedInstance].onceKeysArray containsObject:key]) {
        [[YKUserDefault sharedInstance].onceKeysArray addObject:key];
    }
    [self setCacheValue:value forKey:key];
}

+ (void)setCacheValue:(id)value forKey:(NSString *)key
{
    [[YKUserDefault sharedInstance].userdefaults setObject:[YKUserDefault sharedInstance].onceKeysArray forKey:@"onceKeysArray"];
    if (![[YKUserDefault sharedInstance].normalKeysArray containsObject:key]) {
        [[YKUserDefault sharedInstance].normalKeysArray addObject:key];
    }
    [[YKUserDefault sharedInstance].userdefaults setObject:[YKUserDefault sharedInstance].normalKeysArray forKey:@"normalKeysArray"];
    [[YKUserDefault sharedInstance].userdefaults setValue:value forKey:key];
}

+ (void)clearAllOnceValue
{
    [[YKUserDefault sharedInstance].onceKeysArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [[YKUserDefault sharedInstance].userdefaults setObject:nil forKey:obj];
    }];
}

+ (void)clearAllValue
{
    [[YKUserDefault sharedInstance].normalKeysArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [[YKUserDefault sharedInstance].userdefaults setObject:nil forKey:obj];
    }];
}

+ (id)getObjectForKey:(NSString *)key;
{
    return [[YKUserDefault sharedInstance].userdefaults objectForKey:key];
}
#pragma mark -----------getter/setter------------
- (NSMutableArray<NSString *> *)onceKeysArray
{
    if (!_onceKeysArray) {
        _onceKeysArray = [self.userdefaults arrayForKey:@"onceKeysArray"]?[[self.userdefaults arrayForKey:@"onceKeysArray"] mutableCopy]:[NSMutableArray array];
    }
    return _onceKeysArray;
}

- (NSMutableArray<NSString *> *)normalKeysArray
{
    if (!_normalKeysArray) {
        _normalKeysArray = [self.userdefaults arrayForKey:@"normalKeysArray"]?[[self.userdefaults arrayForKey:@"normalKeysArray"] mutableCopy]:[NSMutableArray array];
    }
    return _normalKeysArray;
}

- (NSUserDefaults *)userdefaults
{
    if(!_userdefaults)
    {
        _userdefaults = [NSUserDefaults standardUserDefaults];
    }
    return _userdefaults;
}
@end
